#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Time    : 24-5-24 下午15:37
@Author  : zhangweihao
@File    : tasks.py
'''
import json
import logging
from ext.flask_marsh import ma, BaseMarshmallow
from marshmallow import fields
from model.auto_model.abnormal_check import AbnormalPrivacyAiModelModel,AbnormalPrivacyDataModel
from flask import g, session
type_dict = {
        'all':'总数数量异常', 
        'level':'高危数据异常', 
        'identity':'身份信息数量异常',
        'property':'财产信息数量异常', 
        'account':'账户信息数量异常',
        'credit':'信用风险数量异常', 
        'financial':'金融交易数量异常',
        'authentication':'鉴别信息数量异常',
        # 'derived':'衍生信息数量异常',
        'reputational':'声誉风险数量异常',
        'encrypted':'加密数据数量异常',
        'data_size':'数据量大小异常'
                 }

class AbnormalPrivacyAiModelSchema(ma.SQLAlchemyAutoSchema, BaseMarshmallow):
    model_name = fields.Method('get_model_name')
    final_model_time = fields.DateTime(format='%Y-%m-%d %H:%M:%S')
    img = fields.Method('get_img')

    class Meta:
        model = AbnormalPrivacyAiModelModel
    
    def get_model_name(self, obj):
        return type_dict.get(obj.model_type,'无分类')
    
    def get_img(self, obj):
        return 'static/AImodel/' + str(obj.model_type) + '.png'

class AbnormalPrivacyDataModelSchema(ma.SQLAlchemyAutoSchema, BaseMarshmallow):
    time = fields.DateTime(format='%Y-%m-%d %H:%M:%S')
    hour_end = fields.DateTime(format='%Y-%m-%d %H:%M:%S')
    hour_start = fields.DateTime(format='%Y-%m-%d %H:%M:%S')
    type_name = fields.Method('get_type')
    handle_status = fields.Method('get_handle_status')
    source_ips = fields.Method('handle_source_ips')

    class Meta:
        model = AbnormalPrivacyDataModel
    
    def get_type(self, obj):
        
        return type_dict.get(obj.type,'无分类')
    
    def get_handle_status(self, obj):
        status_dict = {0:'未处理',1:'已确认',2:'误报'}

        return status_dict.get(obj.handle_status,'无状态')
    
    def handle_source_ips(self, obj):
        if obj.source_ips:
            return json.loads(obj.source_ips)

class AbnormalPrivacyDataAnalysisSchema(ma.SQLAlchemyAutoSchema, BaseMarshmallow):
    count = fields.Method('get_count')

    # class Meta:
    #     model = AbnormalPrivacyDataModel
    
    def get_count(self, obj):
        logging.error(self.count)

